package demo1228.homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/*
4.编写一个类Book,具有name,price,press,author;
创建5个Book对象存放到ArrayList集合中，
并实现按照price从小到大的顺序排序。
遍历ArrayList集合，输出每一个对象，展示对象的信息。
 */
public class Demo_04 {
    public static void main(String[] args) {
        List<Book> list = new ArrayList<>();
        list.add(new Book("呐喊", 28, "人民出版社", "鲁迅"));
        list.add(new Book("追风筝的人", 24, "机械工业出版社", "卡塞尼.胡德勒"));
        list.add(new Book("群山回响", 14, "机械工业出版社", "卡塞尼.胡德勒"));
        list.add(new Book("围城", 0, "清华出版社", "钱钟书"));
        list.add(new Book("英语题目三千", 45, "外国语研究出版社", "新东方"));

        Collections.sort(list);

        for (Book book : list) {
            System.out.println(book);
        }


    }
}

class Book implements Comparable<Book> {
    private String name;
    private Integer price;
    private String press;
    private String author;

    public Book(String name, Integer price, String press, String author) {
        this.name = name;
        this.price = price;
        this.press = press;
        this.author = author;
    }

    public Book() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Objects.equals(name, book.name) &&
                Objects.equals(price, book.price) &&
                Objects.equals(press, book.press) &&
                Objects.equals(author, book.author);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price, press, author);
    }


    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", press='" + press + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    @Override
    public int compareTo(Book o) {
        if (price < o.price) {
            return -1;

        } else if (this.price > o.price) {
            return 1;
        } else {
            return 0;
        }
    }
}

